package ah.szxy.service.impl;

import ah.szxy.feign.OrderFeignService;
import ah.szxy.feign.StorageFeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

/**
 * 远程调用接口
 *
 * @author caohaiyang
 * @create 2020-09-17 下午 05:35
 */
@Service
public class BusinessServiceApi {

    @Autowired
    private RestTemplate restTemplate;//使用rpc远程调用

    @Autowired
    private StorageFeignService storageFeignService;//使用Feign进行远程调用

    @Autowired
    private OrderFeignService orderFeignService;

    private static final Logger logger = LoggerFactory.getLogger(BusinessServiceApi.class);

    /*//库存扣减操作 --rpc调用
    public ResponseEntity<Void> deduct( String productNo, Integer count){
        ResponseEntity<Void> responseEntity = restTemplate.getForEntity(
                "http://storage-service/deduct/{productNo}/{count}",
                Void.class,
                productNo, count);
        if (responseEntity.getStatusCode()== HttpStatus.OK){
            logger.info("远程调用库存扣减成功, 扣减的商品编号为{}, 数量为{} ",productNo,count);
        }else {
            logger.info("非常抱歉, 扣减失败!请观察远程调用方法是否有误~");
        }
        return ResponseEntity.ok().build();
    }*/
    //库存扣减操作 --feign调用
    public ResponseEntity<Void> deduct( String productNo, Integer count){
        ResponseEntity<Void> responseEntity = storageFeignService.deduct(productNo, count);
        if (responseEntity.getStatusCode()== HttpStatus.OK){
            logger.info("远程调用库存扣减成功, 扣减的商品编号为{}, 数量为{} ",productNo,count);
        }else {
            logger.info("非常抱歉, 扣减失败!请观察远程调用方法是否有误~");
        }
        return ResponseEntity.ok().build();
    }

    /*//订单新增操作  使用rpc调用方式
    public ResponseEntity createOrder(String userId, String productNo, int count){
        ResponseEntity<Void> responseEntity = restTemplate.getForEntity(
                "http://order-service/createOrder/{userId}/{productNo}/{count}",
                Void.class,
                userId, productNo, count);
        if (responseEntity.getStatusCode()== HttpStatus.OK){
            logger.info("远程调用创建订单成功, 用户为{}, 商品编号为{}, 数量为{} ",userId, productNo, count);
        }else {
            logger.info("非常抱歉, 创建订单失败!请观察远程调用方法是否有误~");
        }

        return ResponseEntity.ok().build();
    }*/
    //使用feign调用方式
    public ResponseEntity createOrder(String userId, String productNo, int count){
        ResponseEntity<Void> responseEntity = orderFeignService.createOrder(userId, productNo, count);
        if (responseEntity.getStatusCode()== HttpStatus.OK){
            logger.info("远程调用创建订单成功, 用户为{}, 商品编号为{}, 数量为{} ",userId, productNo, count);
        }else {
            logger.info("非常抱歉, 创建订单失败!请观察远程调用方法是否有误~");
        }

        return ResponseEntity.ok().build();
    }
}